#!/usr/bin/env python
# $Id: add_types.py 2 2008-04-04 18:53:47Z benchline $

from page import page
import sys, time, urllib
import db 

class add(page):

	def __init__(self):
		page.__init__(self)
		self.headingstr = "Category Edit"
		self.db1 = db.db()
		self.upcdb1 = db.upcdb()

	def render(self):
		self.logic()
		self.encoding()
		self.html()

	def logic(self):
		if request.has_key("cancel"):
			raise HTTP_REDIRECTION, "type_list.py"
		if request.has_key("add"):
			self.db1.add_type(request["type"][0], request["shelf_life"][0], request["goal_amt"][0], request["goal_unit"][0])
			raise HTTP_REDIRECTION, "add_types.py?%s" % urllib.urlencode({"m":"Type '%s' has been added." % request["type"][0]})
		if request.has_key("update"):
			self.db1.update_type(request["id"][0], request["type"][0], request["shelf_life"][0], request["goal_amt"][0], request["goal_unit"][0])
			raise HTTP_REDIRECTION, "type_list.py?%s" % urllib.urlencode({"m":"Type '%s' has been updated." % request["type"][0]})
		if request.has_key("delete") and request.has_key("id"):
			self.db1.delete_type(request["id"][0])
			raise HTTP_REDIRECTION, "type_list.py?%s" % urllib.urlencode({"m":"Type '%s' has been deleted." % request["type"][0]})
		if request.has_key("id"):
			self.id = request["id"][0]
			self.type, self.shelf_life, self.goal_amt, self.goal_unit = self.db1.get_type_info_from_id(self.id)
		else:
			self.id = ""
			self.type, self.shelf_life, self.goal_amt, self.goal_unit = ("", "", "", "")

	def form(self):
		form = """
		<form name="add" method="POST" action="add_types.py">
		<input type=hidden name=id value=%s>
		<table style="text-align: left; width: 40%%;" border="0" cellpadding="2" cellspacing="2">
		<tbody>
		<tr>
			<td>Type</td>
			<td><input type=text name=type value='%s'></td>
		</tr>
		<tr>
			<td>Shelf Life (in months)</td>
			<td><input type=text name=shelf_life value='%s'></td>
		</tr>
		<tr>
			<td>Goal Amt</td>
			<td><input type=text name=goal_amt value='%s'></td>
		</tr>
		<tr>
			<td>Goal Unit (i.e. oz,gal,etc)</td>
			<td><input type=text name=goal_unit value='%s'></td>
		</tr>
			<script>document.forms.add.type.focus();</script>
		</tbody>
		</table>
		""" % (self.id, self.type, self.shelf_life, self.goal_amt, self.goal_unit) 
		print form
		if request.has_key("id"):
			print """<input name=update type=submit value="Update Category">
			<input type=submit name=cancel value="Menu">
			<img width="104px" height="0px"><input type=submit name=delete value="Delete" onclick="return confirm('Press OK to delete this type.');">"""
		else:
			print """<input name=add type=submit value="Add Type">
			<input type=submit name=cancel value="Menu">"""
		print """
		</form>
		""" 

	def message(self):
		if request.has_key("m"):
			message = request["m"][0]
			if message:
				print message

	def body(self):
		self.start_body()
		self.heading()
		self.form()
		self.message()
		self.footer()
		self.end_body()

class runner:
	def _test(self):
		add().render()

	def _doctest(self):
		import doctest
		doctest.testmod()

if __name__ == "__main__":
	r = runner()
	getattr(r, "_"+sys.argv[1])()
else:
	add().render()
